﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Drawing;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing.Imaging;
using RA.UI.Messages;
using RA.UI.Model;
using GalaSoft.MvvmLight.Ioc;
using RA.Services;
using RA.Common;

namespace RA.UI.ViewModel
{
    public class ImageDisplayViewModel : ViewModelBase
    {
        ImageProcessService _recognitionAgent;
        Bitmap _originImage;
        bool _templatesShown = false;

        public ImageDisplayViewModel(ImageProcessService recognitionAgent)
        {
            _recognitionAgent = recognitionAgent;


       //     Messenger.Default.Register<RecognitionAgentChangedMessage>(this, (mess) => _recognitionAgent = mess.RecognitionAgent);
            //_recognitionAgent = SimpleIoc.Default.GetInstance<IRecognitionAgent>();

            Messenger.Default.Register<OpenImageMessage>(this, (mess) => 
                {
                    if (mess.ImagePath == null)
                        return;
                    _originImage = new Bitmap(mess.ImagePath);
                    SourceImage = ConvertBitmapToBitmapImage(_originImage);
                    BroadCastShowImageTemplates();
                }
            );

            Messenger.Default.Register<ChangeAgentMessage>(this, (mess) =>
            {
                _recognitionAgent.ChangeAgent(mess.Agent, _originImage);

                Messenger.Default.Send(new AgentChangedSettingsChangedMessage(Settings.Current));

                if (_templatesShown)
                    SourceImage = ConvertBitmapToBitmapImage(_recognitionAgent.ShowAllTemplates(_originImage));
                else
                    SourceImage = ConvertBitmapToBitmapImage(_originImage);
            }
            );

            Messenger.Default.Register<SettingsChangedMessage>(this, (mess) =>
            {
                _recognitionAgent.SetSettings(mess.Settings);
                BroadCastShowImageTemplates();

                if (_templatesShown)
                    SourceImage = ConvertBitmapToBitmapImage(_recognitionAgent.ShowAllTemplates(_originImage));
                else
                    SourceImage = ConvertBitmapToBitmapImage(_originImage);
            }
            );

            //Messenger.Default.Register<ShowContoursMessage>(this, (mess) =>
            //    {
            //        if (mess.IsShowContorsNeeded)
            //            SourceImage = ConvertBitmapToBitmapImage(_recognitionAgent.GetContours(_originImage));
            //        else
            //            SourceImage = ConvertBitmapToBitmapImage(_originImage);
            //        _templatesShown = false;
            //    }
            //);

            Messenger.Default.Register<ShowTemplateRectangleMessage>(this, (mess) =>
               SourceImage = ConvertBitmapToBitmapImage(_recognitionAgent.ShowTemplate(_originImage, mess.Template)));


            Messenger.Default.Register<ShowTemplateAllRectanglesMessage>(this, (mess) =>
                {
                    SourceImage = ConvertBitmapToBitmapImage(_recognitionAgent.ShowAllTemplates(_originImage));
                    _templatesShown = true;
                });
          //  Messenger.Default.Register<ShowSourceMessage>(this, (mess) => Source = ConvertBitmapToBitmapImage(mess.Bitmap));
           // Messenger.Default.Register<ShowContoursMessage>(this, (mess) => Source = ConvertBitmapToBitmapImage(mess.Bitmap));
        }

        BitmapImage _sourceImage;
        public BitmapImage SourceImage
        {
            get { return _sourceImage; }

            set 
            {
                if(_sourceImage != value)
                {
                    _sourceImage = value;
                    RaisePropertyChanged(() => SourceImage);
                }
            }
        }

        BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
        {
            if (bitmap == null)
                return null;

            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Bmp);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
            bitmapImage.EndInit();

            return bitmapImage;
        }

        void BroadCastShowImageTemplates()
        { 
            if(_recognitionAgent != null)
                Messenger.Default.Send(new ShowImageTemplatesMessage(_recognitionAgent.GetTemplates(_originImage)));
        }
    }
}
